/*

Copyright 2014年10月9日 tngou



Licensed under the Apache License, Version 2.0 (the "License");

you may not use this file except in compliance with the License.

You may obtain a copy of the License at



    http://www.apache.org/licenses/LICENSE-2.0



Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

*/


package net.tngou.c4j.util;   

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;



import net.tngou.c4j.model.Url;



/**   

 * Filename:    GetURL.java   

 * Copyright:   Copyright (c)2014 

 * Company:     rimi  

 * @version:    1.0   

 * @since:      JDK 1.7.0_21  

 * Create at:   2014年10月9日 下午2:44:00   

 * Description:  页面URL

 *   

 * Modification History:   

 * Date             Author      Version     Description   

 * ----------------------------------------------------------------- 

 * 2014年10月9日           陈磊                  1.0         1.0 Version   

 */

public class UrlUtil {

	/**
	 * 取得文档中的URL
	 * @param doc
	 * @return
	 */
	public static Set<Url> GetUrl(Document doc) {
		 Set<Url> links = new  HashSet<>();
		  Elements as = doc.select("a[href]");
		  for (Element link : as) {
			         Url url  =new Url();
			          String urls = link.attr("abs:href").split("#")[0];
			          try {
			        	urls=urls.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
						urls=URLDecoder.decode(urls, "utf-8");//对URL解码
						} catch (UnsupportedEncodingException e) {					
							e.printStackTrace();
					  }
				      url.setUrl(urls);
				      
				      links.add(url);
				
			}
 
		 return links;
	}
//	public static List<Url> GetUrl(String str,UrlDetectorOptions options) {
//		 UrlDetector parser = new UrlDetector(str, options);
//		 return parser.detect();
//	}
	
}
